<?php
namespace App\Http\Controllers\Home;
use App\Models\Agent;
use App\Models\Agentlev;

Class LoginController extends BaseController{

    public function __construct(Agent $agent , Agentlev $agentlev)
    {
        parent::__construct();
        $this->agent = $agent;
        $this->agentlev = $agentlev;
    }

    public function getLogin(){

          return view("home.login.login");
    }

    public function postLogin(){
         $res = $this->agent->homeLogin(request());
        if($res['status']!=1) {
            return redirect()->back()->with('message', $res['msg'].'！ :)');
        }
        if($res['status']==1){
            return redirect(urlHome('index','getIndex'));
        }
    }

    public function postRegister(){
           $id = $_GET['upid'];
           $data = $this->agent->getOne(array('id'=>$id));
           $data = $data->toArray();
           $lev = $this->agentlev->getOne(array('id'=>$_GET['lev']));
           $lev = $lev->toArray();
           return view('home.register')->with('data',$data)->with('lev',$lev);
    }

    /**
     * 退出
     * @return \Illuminate\Http\JsonResponse
     */
    public function loginOut(){
        $res = $this->agent->getLogout();
        return redirect(urlHome('login','getLogin'));

    }

    public function postRegisterProcess(){
         $res = $this->agent->addAgentFrom(request());
         return redirect()->back()->with('message', $res['msg'])->with('status',$res['status']);
    }
}